<template>
    <t-dialog v-model:visible="visible" :on-confirm="onConfirm" :closeBtn="false" header="传感器新增">
        <t-form ref="form" :data="formData" :rules="rules" @reset="onReset" @submit="onSubmit" label-width="70px" resetType='initial'>
            <t-form-item  label="传感器" name="name">
                <t-input v-model="formData.name"></t-input>
            </t-form-item>

            <t-form-item  label="从站编号" name="station">
                <t-input-number v-model="formData.station" :min="1" :max="255"></t-input-number>
            </t-form-item>
            <t-form-item label="功能码" name="code">
                <t-select v-model="formData.code" class="demo-select-base" clearable filterable :options="options1">
                   
                </t-select>
            </t-form-item>
            <t-form-item label="起始地址" name="address">
                <t-input-number v-model="formData.station" :min="1"></t-input-number>
            </t-form-item>
            <t-form-item label="读取数量" name="num">
                <t-input-number v-model="formData.num" :min="1"></t-input-number>
            </t-form-item>
            <t-form-item  label="单位" name="unit">
                <t-input v-model="formData.unit"></t-input>
            </t-form-item>
            <t-form-item label="系数" name="scale">
                <t-input-number v-model="formData.scale" :min="0.001"></t-input-number>
            </t-form-item>
        </t-form>
    </t-dialog>
</template>
<script setup>
import {reactive, ref} from 'vue';
import { addProductProfile } from '@/api/api';
import { NotifyPlugin, } from 'tdesign-vue-next';
const formData = reactive({
    name:'',
    station:1,
    code:3,
    address:1,
    num:1,
    unit:'',
    scale:1.0
})
const modbus = ref({})

const options1 = [
    { label: '读线圈状态', value: 1 },
  { label: '读离散输入', value: 2 },
  { label: '读保持寄存器', value: 3 },
  { label: '读输入寄存器', value: 4 },
]
    

const form = ref()
const visible = ref(false);
const checkName = () => {
    if(formData.name===''){
        return { result: false, message: '名称不能为空', type: 'error' }
    }
    return { result: true, type: 'success' }
}

const rules = {
  name: [
  { validator: checkName, },
]
};


const onSubmit = ({ validateResult, firstError, e }) => {
  e.preventDefault();
  if (validateResult === true) {
    let data = {
        product:modbus.value.product,
        name:formData.name,
        code:formData.code,
        address:formData.address,
        station:formData.station,
        num:formData.num,
        unit:formData.unit,
        scale:formData.scale
    }
    addProductProfile(data).then(res=>{
        if(res.code===200){
            NotifyPlugin.success({ title: '成功', content: '新增成功', closeBtn:true })
            emit("query")
            visible.value = false
        }
    })
  } 
};


const onReset = () => {
    form.value.reset();
};


 


 const onConfirm = () => {
    visible.value = false;
    form.value.submit();
 }

const open = (row) =>{
    form.value.reset();
    modbus.value.product = row.product
    modbus.value.type = row.type
    visible.value = true
}

const emit = defineEmits(['query'])

defineExpose({
    open,
})

</script>